<?php
class VendorsMaster{

	function __construct(){
	}

	function __destruct(){
	}
	
	public function getVendorServicesAttributes($vendor_service_id=NULL){
		if($vendor_service_id)$where.=" AND vsa.vendor_service_id=".$vendor_service_id;
		$qry = "SELECT vsa.* 
				FROM vendor_services_attributes vsa where 1=1 ".$where;
		$rs = MyDB::$db->Execute($qry);
		$vendor_services_attributes = array();
		while ($row = $rs->fetchRow()) {
			$vendor_services_attributes[] = $row;
		}
		return $vendor_services_attributes;
	}
	
	public function getVendorService($id=NULL){
		$where = " WHERE vs.status != '2' ";
		if($id)$where.=" AND vs.id IN(".$id.")";
		$qry = "SELECT vs.*, sm.name, v.name as vendor_name, bm.brand_name 
				FROM vendor_services vs
				LEFT JOIN vendor as v ON v.id = vs.vendor_id 
				LEFT JOIN services_master as sm ON sm.id = vs.service_id 
				LEFT JOIN brand_master as bm ON bm.id = vs.brand_id
				".$where." ORDER BY vs.added DESC";
		$rs = MyDB::$db->Execute($qry);
		$vendor_service = array();
		while ($row = $rs->fetchRow()) {
			$vendor_service[] = $row;
		}
		return $vendor_service;
	}
	
	public function getVendorProductAttributes($id=NULL){
		if($id)$where.=" AND vpa.vendor_product_id=".$id;
		$qry = "SELECT vpa.*, am.title
				FROM vendor_product_attributes vpa, attribute_master am where vpa.attribute_id=am.id  ".$where;
		$rs = MyDB::$db->Execute($qry);
		$vendor_product_attributes = array();
		while ($row = $rs->fetchRow()) {
            $rowUnit = CommonUtility::getDataById($row['unit_id'], 'unit_master');
            if( $rowUnit ){
                $row['unit'] = $rowUnit['title'];
            }
            $row['attribute_name'] = $row['title'];
			$vendor_product_attributes[] = $row;
		}
		return $vendor_product_attributes;
	}
	
	public function getVendorProduct($id=NULL){
		$where = " WHERE vp.status != '2' ";
		if($id)$where.=" AND vp.id=".$id;
		$qry = "SELECT vp.*, p.product_name, v.name as vendor_name, bm.brand_name 
				FROM vendor_product vp
				LEFT JOIN vendor as v ON v.id = vp.vendor_id 
				LEFT JOIN product as p ON p.id = vp.product_id 
				LEFT JOIN brand_master as bm ON bm.id = vp.brand_id
				".$where." ORDER BY vp.added DESC";
		$rs = MyDB::$db->Execute($qry);
		$vendor_product = array();
		while ($row = $rs->fetchRow()) {
			$vendor_product[] = $row;
		}
		return $vendor_product;
	}
			
	/* This function is used to get all the active services
	@Input : interger $id
	@Output : array $services
	*/
	public function getServices($id = NULL){
		//if(!empty($id)) $where = " where v.id ";
		$where = " WHERE vsm.status = 1 ";
		$qry = "SELECT * FROM vendor_service_master vsm".$where." ORDER BY vsm.service ASC";
		$rs = MyDB::$db->Execute($qry);
		$services = array();
		while ($row = $rs->fetchRow()) {
			$services[] = $row;
		}
		return $services;
	}
	
	/* This function is used to get all the active countries
	@Input : interger $id
	@Output : array $countries
	*/
	public function getCountries($id = NULL){
		$where = " where cm.status = 1 ";
		$qry = "SELECT * FROM country_master cm".$where." ORDER BY cm.id ASC";
		$rs = MyDB::$db->Execute($qry);
		$countries = array();
		while ($row = $rs->fetchRow()) {
			$countries[] = $row;
		}
		return $countries;
	}
	
	/* This function is used to get all the active states
	@Input : interger $id
	@Input : interger $country_id
	@Output : array $states
	*/
	public function getStates($id = NULL,$country_id =1){
		$where = " where country_id = ".$country_id." AND sm.status = 1 ";
		$qry = "SELECT * FROM state_master sm".$where." ORDER BY sm.id ASC";
		$rs = MyDB::$db->Execute($qry);
		$states = array();
		while ($row = $rs->fetchRow()) {
			$states[] = $row;
		}
		return $states;
	}
	
	/* This function is used to get all the active cities
	@Input : interger $id
	@Input : interger $country_id
	@Input : interger $state_id
	@Output : array $cities
	*/
	public function getCities($id = NULL,$country_id = 1,$state_id = 1){
		$where = " where country_id = ".$country_id." AND state_id = ".$state_id." AND cm.status = 1 ";
		$qry = "SELECT * FROM city_master cm".$where." ORDER BY cm.name ASC";
		$rs = MyDB::$db->Execute($qry);
		$cities = array();
		while ($row = $rs->fetchRow()) {
			$cities[] = $row;
		}
		return $cities;
	}
	
	/* This function is used to get all services of vendors
	@Input : interger $id
	@Output : array $vendor_services
	*/
	public function getVendorsServices($id=NULL){
		$where = " WHERE vs.vendor_id = ".$id;
		$qry = "SELECT vsm.id as service_id, vsm.service
				FROM vendor_services vs 
				LEFT JOIN vendor_service_master vsm ON vsm.id = vs.service_id
				".$where;
		
		$rs = MyDB::$db->Execute($qry);
	
		$vendors_services = array();
	
		while ($row=$rs->fetchRow()) {
			$vendors_services[]=$row;
		}
		return $vendors_services;
	}
	
	
	/* This function is used to get all services cities of vendors
	@Input : interger $id
	@Output : array $vendor_services
	*/
	public function getVendorsServicesCities($id=NULL){
		$where = " WHERE vsc.vendor_id = ".$id;
		$qry = "SELECT vsc.city_id , cm.name as city_name
				FROM vendor_service_city vsc 
				LEFT JOIN city_master cm ON cm.id = vsc.city_id
				".$where. " GROUP BY vsc.city_id";
		
		$rs = MyDB::$db->Execute($qry);
	
		$vendors_services_cities = array();
	
		while ($row=$rs->fetchRow()) {
			$vendors_services_cities[]=$row;
		}
		return $vendors_services_cities;
	}

	/* This function is used to get all the vendors
	@Input : interger $id
	@Output : array $vendor
	*/
	public function getVendorsDetail($id=NULL){
		//Logger::log("Enter: AdminMaster::getAdminDetail() | Args: ");
		$where = " WHERE v.status != '2' ";
		//$where = "";
		if(!empty($id)) $where = " where v.id = ".$id;
		$qry = "SELECT v.*, sm.name as state_name, cm.name as city_name
				FROM vendor v 
				LEFT JOIN state_master sm ON sm.id = v.state
				LEFT JOIN city_master cm ON cm.id = v.city
				".$where." 
				GROUP BY v.id
				ORDER BY v.modified DESC";
				
		$rs = MyDB::$db->Execute($qry);
		$vendor = array();
		while ($row=$rs->fetchRow()) {
			$vendor[]=$row;
		}
		return $vendor;
		//Logger::log("Exit: AdminMaster::getAdminDetail()");
	}
	
	/* This function is used to update the vendors services city status
	@Input : interger $id
	@Input : interger $status
	@Output : NULL
	*/
	public function vendorServicesCityStatusUpdate($vendor_id,$status=1){
		if(empty($vendor_id)) return;
		$Query = "UPDATE vendor_service_city SET status=".MyDB::$db->qstr($status)." WHERE vendor_id=".MyDB::$db->qstr($vendor_id);
		MyDB::$db->Execute($Query);
	}
	
	/* This function is used to update the vendors services status
	@Input : interger $id
	@Input : interger $status
	@Output : NULL
	*/
	public function vendorServicesStatusUpdate($vendor_id,$status=1){
		if(empty($vendor_id)) return;
		$Query = "UPDATE vendor_services SET status=".MyDB::$db->qstr($status)." WHERE vendor_id=".MyDB::$db->qstr($vendor_id);
		MyDB::$db->Execute($Query);
	}
	
	/* This function is used to update the vendors status
	@Input : interger $id
	@Input : interger $status
	@Output : NULL
	*/
	public function vendorStatusUpdate($id,$status=1){
		if(empty($id)) return;
		
		self::vendorServicesCityStatusUpdate($id,$status);
		self::vendorServicesStatusUpdate($id,$status);
		
		$Query = "UPDATE vendor SET status=".MyDB::$db->qstr($status)." WHERE id=".MyDB::$db->qstr($id);
		MyDB::$db->Execute($Query);
	}
	
	/* This function is used to delete the vendors. Here we will set status as 2.
	@Input : interger $id
	@Output : NULL
	*/
	public function vendorDelete($id){
		if(empty($id)) return;
		
		self::vendorServicesCityStatusUpdate($id,2);
		self::vendorServicesStatusUpdate($id,2);
		self::vendorStatusUpdate($id,2);
		
		//$Query = "DELETE FROM vendor where id=".MyDB::$db->qstr($id);
		//MyDB::$db->Execute($Query);
		//$Query = "delete from user_details where user_id=".MyDB::$db->qstr($id);
		//MyDB::$db->Execute($Query);
	}
		
	public function checkVendorEmail($email,$id=NULL){
		//Logger::log("Enter: AdminMaster::getAdminDetail() | Args: ");
		if(!empty($id)) $where = " AND id<>".MyDB::$db->qstr($id);
		$qry = "select count(id) as cnt from vendor where email_id=".MyDB::$db->qstr($email).$where;
		$rs = MyDB::$db->Execute($qry);
		if( $rs ){
			$arr = $rs->fetchRow();
			$cnt =  $arr['cnt'];
			return ($cnt == 0 )?true:false;
		}
		return false;
		//Logger::log("Exit: AdminMaster::getAdminDetail()");
	}
	
	public function checkVendorLogin($login_id,$id=NULL){
		//Logger::log("Enter: AdminMaster::getAdminDetail() | Args: ");
		if(!empty($id)) $where = " AND id<>".MyDB::$db->qstr($id);
		$qry = "select count(id) as cnt from vendor where login_id=".MyDB::$db->qstr($login_id).$where;
		$rs = MyDB::$db->Execute($qry);
		if( $rs ){
			$arr = $rs->fetchRow();
			$cnt =  $arr['cnt'];
			return ($cnt == 0 )?true:false;
		}
		return false;
		//Logger::log("Exit: AdminMaster::getAdminDetail()");
	}
}	
?>